﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.CommonBase.Integration.DAO.Impl;
using Demo.Services.Contracts.DTO.Infos;
using Demo.CommonBase.DTO.Infos;
using Demo.Services.Contracts.DTO.Filters;
using System.Data;
using Demo.Services.Integration.DAO.IntegrationComponents.Helpers;
using Demo.Services.Integration.DAO.IntegrationComponents.Mappings;
using System.Data.SqlClient;
using Demo.CommonBase.Logging;
using Demo.Services.Integration.DAO.Contracts.Exceptions;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.CommonBase.DTO.Filters;

namespace Demo.Services.Integration.DAO.Impl
{
  public   class OperationDAO :  DAOBase
    {
      public OperationPagingInfo GetOperations(OperationFilter filter)
      {
          OperationPagingInfo operations = null;
          try
          {
              Logger.Info();
              IList<IDbDataParameter> parameters = OperationDAOHelper.GetOperationsParam(filter);
              DataSet ds = Retrieve("Operation_R", parameters);
              if (ValidateDataSet(ds))
              {
                  operations = OperationDAOMapper.GetOperationList(ds);
              }

          }
          catch (SqlException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "OperationDAO", ex);
          }
          catch (DataException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                        "OperationDAO", ex);
          }
          catch (Exception ex)
          {
              Logger.Error(ex);
              throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
          }
          return operations;
      }

      public ResourcePagingInfo GetOperationResources(ResourceFilter filter)
      {
          ResourcePagingInfo result = null;
          try
          {
              Logger.Info();
              IList<IDbDataParameter> parameters = OperationDAOHelper.GetOperationResourceParam(filter);
              DataSet ds = Retrieve("Operation_R_Resources", parameters);
              if (ValidateDataSet(ds))
              {
                  result = OperationDAOMapper.GetOperationResourcesList(ds);
              }

          }
          catch (SqlException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "OperationDAO", ex);
          }
          catch (DataException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                        "OperationDAO", ex);
          }
          catch (Exception ex)
          {
              Logger.Error(ex);
              throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
          }
          return result;
      }

      public ResourceInfo GetResourceLineTime(int id)
      {
          ResourceInfo result = null;
          try
          {
              Logger.Info();
              IList<IDbDataParameter> parameters = OperationDAOHelper.GetResourceLineTimeParam(id);
              DataSet ds = Retrieve("Resource_ByLineaTiempo", parameters);
              if (ValidateDataSet(ds))
              {
                  result = OperationDAOMapper.GetResourceLineTime(ds);
              }

          }
          catch (SqlException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "OperationDAO", ex);
          }
          catch (DataException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                        "OperationDAO", ex);
          }
          catch (Exception ex)
          {
              Logger.Error(ex);
              throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
          }
          return result;
      }


      public void SaveLineTime(LineTimeInfo lineTime)
      {
          try
          {
              Logger.Info();
              IList<IDbDataParameter> parameters = OperationDAOHelper.SaveLineTimeParam(lineTime);
              Create("LineTime_C", parameters);             

          }
          catch (SqlException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "OperationDAO", ex);
          }
          catch (DataException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                        "OperationDAO", ex);
          }
          catch (Exception ex)
          {
              Logger.Error(ex);
              throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
          }
         
      }

      public void UpdateLineTime(LineTimeInfo lineTime)
      {
          try
          {
              Logger.Info();
              IList<IDbDataParameter> parameters = OperationDAOHelper.UpdateLineTimeParam(lineTime);
              Update("LineTime_U", parameters);
          }
          catch (SqlException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "OperationDAO", ex);
          }
          catch (DataException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                        "OperationDAO", ex);
          }
          catch (Exception ex)
          {
              Logger.Error(ex);
              throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
          }

      }

      public void DeleteOperationLineTime(int id)
      {
          try
          {
              Logger.Info();
              IList<IDbDataParameter> parameters = OperationDAOHelper.DeleteOperationLineTimeParam(id);
              Delete("Operation_D_LineTime", parameters);
          }
          catch (SqlException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "OperationDAO", ex);
          }
          catch (DataException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                        "OperationDAO", ex);
          }
          catch (Exception ex)
          {
              Logger.Error(ex);
              throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
          }

      }


      public void DeleteLineTime(int id)
      {
          try
          {
              Logger.Info();
              IList<IDbDataParameter> parameters = OperationDAOHelper.DeleteLineTimeParam(id);
              Delete("LineTime_D", parameters);
          }
          catch (SqlException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "OperationDAO", ex);
          }
          catch (DataException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                        "OperationDAO", ex);
          }
          catch (Exception ex)
          {
              Logger.Error(ex);
              throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
          }

      }

      public bool ValidationOvarlapDateResource(LineTimeInfo lineTimeInfo)
      {
          bool result;
          try
          {
              IList<IDbDataParameter> parameters = OperationDAOHelper.ValidationOvarlapDateParam(lineTimeInfo);
              DataSet ds = Retrieve("Recurso_OverLap_Date", parameters);
              result = ValidateDataSet(ds);
          }
          catch (SqlException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "c", ex);
          }
          catch (DataException ex)
          {
              Logger.Error(ex);
              throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                                             "ValidationOvarlapDateParam", ex);
          }
          catch (Exception ex)
          {
              Logger.Error(ex);
              throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
          }
          return result;
      }

    }
}
